package com.shanjupay.merchant.controller;


import com.shanjupay.common.domain.Result;
import com.shanjupay.merchant.service.SmsService;
import com.shanjupay.merchant.service.api.MerchantService;
import com.shanjupay.merchant.service.dto.MerchantDTO;
import com.shanjupay.merchant.service.param.MerchantParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author xiams
 * @date 2021/5/19 14:27
 */

@RestController
@RequestMapping("/merchant")
@Api("商户中心服务")
public class MerchantController {

    @Reference
    private MerchantService merchantService;

    @Autowired
    private SmsService smsService;

    @GetMapping("/queryTest/{id}")
    @ApiOperation("测试服务之间的调用")
    public Result<MerchantDTO> queryMerchantById(@PathVariable("id") Long id) {
        MerchantDTO dto = merchantService.queryMerchantTest(id);
        return Result.success(dto);
    }

    @GetMapping("/send/smsCode")
    @ApiOperation("发送手机验证码(模拟)")
    public Result<Map<String, String>> getSmsCode(@RequestParam String phone) {
        Map<String, String> map = smsService.sendSmsCode(phone);
        return Result.success(map);
    }

    @GetMapping("/check/smsCode")
    @ApiOperation("验证手机验证码")
    public Result<?> validateCode(@RequestParam @ApiParam("验证key") String verificationKey,
                                  @RequestParam @ApiParam("验证码") String verificationCode) {
        smsService.validateCode(verificationKey, verificationCode);
        return Result.success();
    }

    @ApiOperation("获取到手机验证码")
    @GetMapping("/getCachePhone")
    public Result<String> getCodePhoneByCache(@RequestParam @ApiParam("验证key") String verificationKey) {
        String phone = smsService.getCachePhone(verificationKey);
        return Result.success(phone);
    }

    @ApiOperation("商户注册")
    @PostMapping("/register")
    @Transactional(rollbackFor = Exception.class)
    public Result<?> register(@RequestBody MerchantParam param) {
        smsService.validateCode(param.getSmsKey(), param.getSmsCode());
        merchantService.register(param);
        return Result.success();
    }


}
